package com.huafv.flyauth.utils;

import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    private BeanCopyUtils(){

    }

    //复制单个对象
    public static <T> T beanCopy(Object source,Class<T> clazz) throws InstantiationException, IllegalAccessException{
        //创建目标对象

        T vo = clazz.newInstance();
        BeanUtils.copyProperties(source,vo);
        return vo;
    }

    //复制列表数据
    public static <V,T> List<T> copyList(List<V> list,Class<T> clazz){
        List<T> result = list.stream()
                .map(element ->{
                    T vo;
                    try {
                        vo = beanCopy(element,clazz);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    return vo;
                })
                .collect(Collectors.toList());
        return result;
    }
}